#!/bin/bash

if [ $# -lt 1 ];
then
  echo "USAGE: $0 [-daemon] [-name servicename] [-loggc] classname [opts]"
  exit 1
fi

# CYGINW == 1 if Cygwin is detected, else 0.
if [[ $(uname -a) =~ "CYGWIN" ]]; then
  CYGWIN=1
else
  CYGWIN=0
fi

if [ -z "$INCLUDE_TEST_JARS" ]; then
  INCLUDE_TEST_JARS=false
fi

# Exclude jars not necessary for running commands.
regex="(-(test|src|scaladoc|javadoc)\.jar|jar.asc)$"
should_include_file() {
  if [ "$INCLUDE_TEST_JARS" = true ]; then
    return 0
  fi
  file=$1
  if [ -z "$(echo "$file" | egrep "$regex")" ] ; then
    return 0
  else
    return 1
  fi
}

base_dir=$(dirname `pwd`)

if [ ! -d "$CLASSPATH_CONF" ]; then
  CLASSPATH=""
else
  CLASSPATH="$CLASSPATH_CONF"
fi

# classpath addition for release
for file in "$base_dir"/lib/*;
do
  if should_include_file "$file"; then
    CLASSPATH="$CLASSPATH":"$file"
  fi
done

# Log directory to use
if [ "x$LOG_DIR" = "x" ]; then
  LOG_DIR="$base_dir/logs"
fi

# create logs directory
if [ ! -d "$LOG_DIR" ]; then
  mkdir -p "$LOG_DIR"
fi

# Generic jvm settings you want to add
if [ -z "$JAVA_OPTS" ]; then
  JAVA_OPTS=""
fi

# Use default ports
DEFAULT_JAVA_DEBUG_PORT="5005"
DEFAULT_DEBUG_SUSPEND_FLAG="y"

# Set Debug options if enabled
if [ "x$JAVA_DEBUG" == "xtrue" ]; then

    if [ -z "$JAVA_DEBUG_PORT" ]; then
        JAVA_DEBUG_PORT="$DEFAULT_JAVA_DEBUG_PORT"
    fi

    if [ -z "$DEBUG_SUSPEND_FLAG" ]; then
        DEBUG_SUSPEND_FLAG="$DEFAULT_DEBUG_SUSPEND_FLAG"
    fi

    # Use the defaults if JAVA_DEBUG_OPTS was not set
    DEFAULT_JAVA_DEBUG_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=${DEBUG_SUSPEND_FLAG:-n},address=$JAVA_DEBUG_PORT"
    if [ -z "$JAVA_DEBUG_OPTS" ]; then
        JAVA_DEBUG_OPTS="$DEFAULT_JAVA_DEBUG_OPTS"
    fi

    echo "Enabling Java debug options: $JAVA_DEBUG_OPTS"
    JAVA_OPTS="$JAVA_DEBUG_OPTS $JAVA_OPTS"
fi

# Which java to use
if [ -z "$JAVA_HOME" ]; then
  JAVA="java"
else
  JAVA="$JAVA_HOME/bin/java"
fi

# JVM performance options
if [ -z "$JAVA_JVM_PERFORMANCE_OPTS" ]; then
  JAVA_JVM_PERFORMANCE_OPTS="-server -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSInitiatingOccupancyOnly -XX:+ScavengeBeforeFullGC -XX:+CMSScavengeBeforeRemark -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC"
fi


while [ $# -gt 0 ]; do
  COMMAND=$1
  case $COMMAND in
    -name)
      DAEMON_NAME=$2
      shift 2
      ;;
    -loggc)
      if [ -z "$JAVA_GC_LOG_OPTS" ]; then
        GC_LOG_ENABLED="true"
      fi
      shift
      ;;
    -daemon)
      DAEMON_MODE="true"
      shift
      ;;
    *)
      break
      ;;
  esac
done

# GC options
GC_FILE_SUFFIX='-gc.log'
GC_LOG_FILE_NAME=''
if [ "x$GC_LOG_ENABLED" = "xtrue" ]; then
  GC_LOG_FILE_NAME=$DAEMON_NAME$GC_FILE_SUFFIX
  JAVA_GC_LOG_OPTS="-Xloggc:$LOG_DIR/$GC_LOG_FILE_NAME -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps "
fi

# If Cygwin is detected, classpath is converted to Windows format.
(( CYGWIN )) && CLASSPATH=$(cygpath --path --mixed "${CLASSPATH}")

# Launch mode
if [ "x$DAEMON_MODE" = "xtrue" ]; then
  nohup $JAVA $JAVA_HEAP_OPTS $JAVA_JVM_PERFORMANCE_OPTS $JAVA_GC_LOG_OPTS $JAVA_JMX_OPTS $JAVA_LOG4J_OPTS -cp $CLASSPATH $JAVA_OPTS "$@" >/dev/null 2>&1 &
else
  exec $JAVA $JAVA_HEAP_OPTS $JAVA_JVM_PERFORMANCE_OPTS $JAVA_GC_LOG_OPTS $JAVA_JMX_OPTS $JAVA_LOG4J_OPTS -cp $CLASSPATH $JAVA_OPTS "$@"
fi
